<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2013
 */

namespace com\org\web\tpl\utils {
	 class FileUtil {
		/**
		* name: read_file_content
		* input:
		*		@var _file_path  as variant string contain the relative file path on the server
		* purpose: read all the content of the file
		* output:
		*		@return content of the file as a variant string
		* remarks: none
		*/
		public static function readFileContent($filePath)
		{
			if($filePath == NULL || trim($filePath) == "")
				throw new \Exception("FileUtil::readFileContent($filePath): _file cannot be null or empty.");
			if(file_exists($filePath) === false)
				throw new \Exception("FileUtil::readFileContent($filePath): _file=" . $filePath . " does not exist.");
			if(is_readable($filePath) === false)
				throw new \Exception("FileUtil::readFileContent($filePath): could not read _file=" . $filePath . ".");
			$content = file_get_contents($filePath);
			if($content === false)
				throw new \Exception("FileUtil::readFileContent($filePath): could not open _file=" . $filePath . ".");
			return $content;
		}

		/**
		* name: parseIncludedFile
		* input:
		*		@var reference _content  as variant string contain the included file url
		* purpose: parse any encounter included file and replace its content to including instruction at the right position in the template file
		* output:
		*		@return parsed variant string
		* remarks: none
		*/
		public static function parseIncludedFile($url)
		{
			if (strcmp(trim($url), "") === 0)
				return "Empty url.";
			ob_start();
			include($url);
			$content = ob_get_contents();
			ob_end_clean();

			return $content;
		}
	 }
}
?>